﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Jewelnoid
{
    class SpecialPowerManager
    {
        private Texture2D specialPowersTexture;
        
        public List<SpecialPower> specialPowerList;

        public SpecialPowerManager(Texture2D specialPowersTexture)
        {
            this.specialPowersTexture = specialPowersTexture;

            specialPowerList = new List<SpecialPower>();
        }

        public void createPower(Vector2 position, int numberBlocksErased)
        {
            SpecialPower specialPower = null;

            Random rand = new Random();
            int perc = rand.Next(100);
            
            switch (numberBlocksErased)
            {
                case 1:
                    if (perc < 2)
                    {
                         specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerUp());
                    }
                    else if (perc < 8)
                    {
                        specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerDown());
                    }
                    break;

                case 2:
                    if (perc < 4)
                    {
                        if (perc < 2)
                        {
                            specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerUp());
                        }
                        else
                        {
                            specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerDown());
                        }
                    }
                    break;

                case 3:
                    if (perc < 2)
                    {
                        specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerDown());
                    }
                    else if (perc < 8)
                    {
                        specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerUp());
                    }
                    break;

                case 4:
                    if (perc < 1)
                    {
                        specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerDown());
                    }
                    else if (perc < 16)
                    {
                        specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerUp());
                    }
                    break;

                case 5:
                    if (perc < 32)
                    {
                        specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerUp());
                    }
                    break;

                case 6:
                    if (perc < 64)
                    {
                        specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerUp());
                    }
                    break;

                default:
                    specialPower = new SpecialPower(specialPowersTexture, position, getRandomPowerUp());
                    break;
            }

            if (specialPower != null)
            {
                specialPowerList.Add(specialPower);
            }
        }

        public void update(GameTime time)
        {
            foreach (SpecialPower specialPower in specialPowerList)
            { 
                specialPower.update(time);
            }
        }

        public void draw(SpriteBatch spriteBatch)
        {
            foreach (SpecialPower specialPower in specialPowerList)
            {
                specialPower.draw(spriteBatch);
            }
        }

        /// <summary>
        ///     Remove os poderes que saíram da tela da Lista.
        /// </summary>
        /// <param name="windowMaxHeight">Altura máxima da tela</param>
        public void removePowersFromList(float windowMaxHeight)
        {
            List<SpecialPower> removePowerList = new List<SpecialPower>();

            foreach (SpecialPower specialPower in specialPowerList)
            {
                if (specialPower.position.Y - (specialPower.texture.Height / 2) >= windowMaxHeight) //Removendo da lista quando o poder sai da tela
                {
                    removePowerList.Add(specialPower);
                }
            }

            foreach (SpecialPower power in removePowerList)
            {
                specialPowerList.Remove(power);
            }
        }

        /// <summary>
        ///     Retorna o poder que colidiu com a barra ou nulo caso nenhum tenha colidido.
        ///     Retira o poder da lista.
        /// </summary>
        /// <param name="pallet"></param>
        /// <returns>Poder que colidiu com a barra</returns>
        public SpecialPower checkSpecialPowerCollision(Pallet pallet)
        {
            foreach (SpecialPower specialPower in specialPowerList)
            {
                if (pallet.checkCollision(specialPower))
                {
                    specialPowerList.Remove(specialPower);
                    return specialPower;
                }
            }

            return null;
        }

        public void erasePowersList()
        {
            specialPowerList = new List<SpecialPower>();
        }

        private SpecialPower.Power getRandomPowerUp()
        {
            Random rand = new Random();

            return SpecialPower.powerUps[rand.Next(SpecialPower.powerUps.Length)];
        }

        private SpecialPower.Power getRandomPowerDown()
        {
            Random rand = new Random();

            return SpecialPower.powerDowns[rand.Next(SpecialPower.powerDowns.Length)];
        }
    }
}
